//
//  DDStatusRetweetView.swift
//  swift微博
//
//  Created by whitcast on 16/7/14.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import SnapKit

class DDStatusRetweetView: UIView {
    //保存更改的bottom
    var bottomConstrain: Constraint?
    //模型vm
    var statuesViewModel:DDStatuesViewModel? {
        didSet{
            //卸载约束
            self.bottomConstrain?.uninstall()
            
            contentLab.text = statuesViewModel?.retweeted_statusModel?.text
            
            
            
            if statuesViewModel?.retweeted_statusModel?.pic_urls?.count > 0 {
                //取消隐藏
                picView.hidden = false
                picView.pic_urlsArr = statuesViewModel?.retweeted_statusModel?.pic_urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrain = make.bottom.equalTo(picView.snp_bottom).constraint
                })
                
            } else {
                //隐藏
                picView.hidden = true

                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.bottomConstrain = make.bottom.equalTo(contentLab.snp_bottom).constraint
                })
            }
        }
    }
       //文字内容
    lazy var contentLab: UILabel = {
    
        let lab = UILabel()
        
        lab.font = UIFont.systemFontOfSize(14)
        //背景颜色
        lab.backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        lab.numberOfLines = 0
        
        return lab
    
    }()
    //图片视图
    lazy var picView: DDPIcView = {
    
        let picV = DDPIcView()
        
        return picV
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //初始化视图
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //创建UI和约束
    func setUpUI() {
        //间距
        let margin = 5
        //添加
        addSubview(contentLab)
        addSubview(picView)
        //约束
        contentLab.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(margin)
            make.leading.equalTo(self.snp_leading).offset(margin)
            make.trailing.equalTo(self.snp_trailing).offset(-margin)
        }
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLab.snp_bottom).offset(margin)
            make.leading.equalTo(contentLab)
        }
    
        self.snp_makeConstraints { (make) -> Void in
            
          self.bottomConstrain = make.bottom.equalTo(picView.snp_bottom).offset(margin).constraint
        }
    }
}
